using DA.AnimGraph;
using System.Collections.Generic;
using System.IO;

namespace DA.AnimGraph.Editor {

    [AGTool("Tool", "文件复制后/重新生成当前文件和Graph的uuid(复制后必用，建议用完重新打开此编辑窗口)")]
    public class Tool_ResetUuidForCopy : IAGTool {
        public void Execute(AGEContext ageContext) {
            var handle = ageContext.assetHandle;
            handle.ResetSOUuid();
            handle.Save();
        }
    }

    [AGBatchTool("Tool", "文件复制后/批量重新生成文件和Graph的uuid(复制后必用，建议先关掉所有编辑窗口)")]
    public class Tool_BatchResetUuidForCopy : IAGBatchTool {
        public void Execute() {
            var fileTool = AGEGlobal.GetInstance().fileTool;
            var fileList = fileTool.agaList;
            HashSet<AUID> uuidSet = new();
            foreach (var file in fileList) {
                var handle = AGAAssetHandle.Open(file.path);
                if (uuidSet.Contains(handle.uuid)) {
                    handle.ResetSOUuid();
                    handle.Save();
                }
                uuidSet.Add(handle.uuid);
            }
        }
    }

    [AGBatchTool("Tool", "一次性更新/设置文件uuid和格式号")]
    public class Tool_Update_SetFileUuid : IAGBatchTool {
        public void Execute() {
            var fileTool = AGEGlobal.GetInstance().fileTool;
            var fileList = fileTool.agaList;
            foreach (var file in fileList) {
                var handle = AGAAssetHandle.Open(file.path);
                handle.ReloadSOUuid();
                handle.SetFormatVersion();
                handle.Save();
            }
        }
    }

    [AGBatchTool("Tool", "一次性更新/去除导出目录的文件名")]
    public class Tool_Update_ClearPathName : IAGBatchTool {
        public void Execute() {
            var fileTool = AGEGlobal.GetInstance().fileTool;
            var fileList = fileTool.agaList;
            foreach (var file in fileList) {
                var handle = AGAAssetHandle.Open(file.path);
                if (string.IsNullOrEmpty(handle.publishPath)) {
                    continue;
                }
                string directoryPath = Path.GetDirectoryName(handle.publishPath);
                // 添加尾部斜杠（根据需要）
                if (!string.IsNullOrEmpty(directoryPath) && !directoryPath.EndsWith("/")) {
                    directoryPath += "\\";
                }
                handle.publishPath = directoryPath;
                handle.Save();
            }
        }
    }

    [AGTool("Tool", "将全部Slot节点的名字改为slot名")]
    public class Tool_SetSlotNodeName : IAGTool {
        public void Execute(AGEContext ageContext) {
            var handle_root = ageContext.GetRootHandle();
            handle_root.ForeachGraphs(graphHandle => {
                var handle_sub = graphHandle.rootSubGraphHandle;
                handle_sub.ForeachNodes(nodeHandle => {
                    if (nodeHandle.data is AGANode_Slot sNode) {
                        sNode.nodeName = (sNode.modelProperty as AGANodeModel_Slot)?.slotName;
                        nodeHandle.SetDirty();
                    }
                });
            });
            ageContext.SaveIfDirty();
            ageContext.ageEnv.window.ReOpenWindow();
        }
    }
}
